use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
use log4rs::encode::pattern::PatternEncoder;
use log4rs::config::{Appender, Config, Root};

fn count_level(count: u8) -> LevelFilter {
    match count {
        0 => LevelFilter::Info,
        1 => LevelFilter::Debug,
        _ => LevelFilter::Trace
    }
}

pub fn init_log(count: u8) {
    let log_path = "log/mtask.log";
    let level = count_level(count);

    let console_appender = ConsoleAppender::builder()
                            .encoder(Box::new(PatternEncoder::new("[Console] {d} - {l} - {t} - {m}{n}")))
                            .build();
    let file_appender = FileAppender::builder()
                        .encoder(Box::new(PatternEncoder::new("[Console] {d} - {l} - {t} - {m}{n}")))
                        .build(log_path)
                        .unwrap();
    let config = Config::builder()
                .appender(Appender::builder().build("stdout", Box::new(console_appender)))
                .appender(Appender::builder().build("logfile", Box::new(file_appender)))
                .build(
                    Root::builder()
                        .appender("stdout")
                        .appender("logfile")
                        .build(level)
                )
                .unwrap();
    let _ = log4rs::init_config(config).unwrap();
}